//
//  TransitionFromViewController.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/13.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "TransitionFromViewController.h"
#import "CardViewController.h"
#import "CustomPresentationController.h"
#import "TransitionToViewController.h"
#import "PresentingViewController.h"
@interface TransitionFromViewController ()
@end

@implementation TransitionFromViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:self.floatBtn];
}

-(UIButton *)floatBtn
{
    if (!_floatBtn) {
        _floatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _floatBtn.backgroundColor = [UIColor grayColor];
        _floatBtn.titleLabel.numberOfLines = 0;
        _floatBtn.titleLabel.textAlignment = 1;
        _floatBtn.layer.cornerRadius = 30;
        _floatBtn.layer.masksToBounds = YES;
        _floatBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _floatBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _floatBtn.frame = CGRectMake(self.view.frame.size.width-60, self.view.frame.size.height-150, 60, 60);
        [_floatBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_floatBtn setTitle:@"点击\n拖动" forState:UIControlStateNormal];
        [_floatBtn addTarget:self action:@selector(_actionFloatBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_actionPanGesture:)];
        [_floatBtn addGestureRecognizer:pan];
        
    }
    return _floatBtn;
}

-(void)_actionPanGesture:(UIPanGestureRecognizer *)recognizer
{
    // Figure out where the user is trying to drag the view.
    // http://www.jianshu.com/p/be29e46fb2c4
    // 帮助区分 translationInView  locationInView   velocityInView 三个方法
    CGPoint translation = [recognizer translationInView:self.view];
    CGPoint newCenter   = CGPointMake(recognizer.view.center.x + translation.x,
                                      recognizer.view.center.y + translation.y);
    // 限制屏幕范围：
    newCenter.y = MAX(recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.y = MIN(self.view.frame.size.height - recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.view.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self.view];
}

#pragma mark - Actions

- (IBAction)presentNextControllerClicked:(UIButton *)sender
{
    TransitionToViewController *vc = [[TransitionToViewController alloc] init];
    vc.didClickDismissBlock = ^(TransitionToViewController *vc) {
        [vc dismissViewControllerAnimated:YES completion:nil];
    };
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)gotoSwipeFromVC:(UIButton *)sender
{
    [self pushToVC:@"SwipeFromViewController" withTitle:sender.titleLabel.text];
}

- (IBAction)cardVC:(UIButton *)sender
{
    CardViewController *cardVC = [[CardViewController alloc] init];
    CustomPresentationController *customPresentationController = [[CustomPresentationController alloc] initWithPresentedViewController:cardVC presentingViewController:self];
    cardVC.transitioningDelegate = customPresentationController.transitionDelegate;
    [self presentViewController:cardVC animated:YES completion:nil];
}

- (IBAction)_actionPageCover:(UIButton *)sender
{
    [self pushToVC:@"PageCoverViewController" withTitle:sender.titleLabel.text];
}

- (IBAction)_actionPresentPop:(UIButton *)sender
{
    [self pushToVC:NSStringFromClass(PresentingViewController.class) withTitle:sender.titleLabel.text];
}

- (IBAction)_actionMagicTransition:(UIButton *)sender
{
    [self pushToVC:@"MagicMoveCollectionViewController" withTitle:sender.titleLabel.text];
}

-(void)_actionFloatBtnClicked:(UIButton *)sender
{
    [self presentVC:@"RoundDotToViewController" withTitle:sender.titleLabel.text];
}

@end
